import { getStream } from '../Core/connect'
import * as MathUtils from './MathUtils'
import { Vector3 } from './Vector3'
import { Vector2 } from './Vector2'
import { GcjBD } from './GcjBD'

export const Convert = {
    lonLatToUE (position: Array<Vector3>) {
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const message = { function: 'lonLatToUE', sessionId: sessionId, position: position }
            stream.emitUIInteraction(message)
            const getLonLatReady = (data: string) => {
                const returnData = JSON.parse(data)
                if (returnData.sessionId === sessionId) { 
                    resolve(returnData)
                    stream.removeResponseEventListener("convertUEToLonLat")
                }
            }
            stream.addResponseEventListener("convertUEToLonLat", getLonLatReady)
        })
    },

    UETolonLat (position: Array<Vector3>) {
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const message = { function: 'UETolonLat', sessionId: sessionId, position: position }
            stream.emitUIInteraction(message)
            const getLonLatReady = (data: string) => {
                const returnData = JSON.parse(data)
                if (returnData.sessionId === sessionId) { 
                    resolve(returnData)
                    stream.removeResponseEventListener("convertLonLatToUE")
                }
            }
            stream.addResponseEventListener("convertLonLatToUE", getLonLatReady)
        })
    },

    UEToPixel (position: Array<Vector3>) {
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const message = { function: 'UEToPixel', sessionId: sessionId, position: position }
            stream.emitUIInteraction(message)
            const getLonLatReady = (data: string) => {
                const returnData = JSON.parse(data)
                if (returnData.sessionId === sessionId) { 
                    resolve(returnData)
                    stream.removeResponseEventListener("convertUEToPixel")
                }
            }
            stream.addResponseEventListener("convertUEToPixel", getLonLatReady)
        })
    },

    pixelToUE (position: Array<Vector2>,floor:string = 'terrain') {
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const message = { function: 'pixelToUE', sessionId: sessionId, position: position, floor:floor }
            stream.emitUIInteraction(message)
            const getLonLatReady = (data: string) => {
                const returnData = JSON.parse(data)
                if (returnData.sessionId === sessionId) { 
                    resolve(returnData)
                    stream.removeResponseEventListener("convertPixelToUE")
                }
            }
            stream.addResponseEventListener("convertPixelToUE", getLonLatReady)
        })
    },

    pixelToLonLat (position: Array<Vector2>,floor:string = 'terrain') {
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const message = { function: 'pixelToLonLat', sessionId: sessionId, position: position, floor:floor }
            stream.emitUIInteraction(message)
            const getLonLatReady = (data: string) => {
                const returnData = JSON.parse(data)
                if (returnData.sessionId === sessionId) { 
                    resolve(returnData)
                    stream.removeResponseEventListener("convertPixelToLonLat")
                }
            }
            stream.addResponseEventListener("convertPixelToLonLat", getLonLatReady)
        })
    },

    GCJTolonLat (position: Array<Vector3>): Array<Vector3> { // 高德转84
        const lists: Array<Vector3> = []
        position.forEach((v: Vector3) => {
           const gcjbd = new GcjBD(v)
            const dd = gcjbd.gcj_decrypt()
            const newcoordinate = new Vector3(dd.lon, dd.lat, dd.height)
            lists.push(newcoordinate)
          })
          return lists
    },

    BDTolonLat(position: Array<Vector3>) { // 百度转84
        const lists: Array<Vector3> = []
        position.forEach((v: Vector3) => {
           const gcjbd = new GcjBD(v)
            const dd = gcjbd.bd_decrypt()
            const newcoordinate = new Vector3(dd.lon, dd.lat, dd.height)
            lists.push(newcoordinate)
          })
          return lists
    },
    lonLatToty(point: Vector3): Vector3 {
        const a = 6378137.0;  
        const f = 1 / 298.257223563; 
        const lon0 = 105.0; 
        const k0 = 1;  
        const lonA = point.x;  
        const latA = point.y;  
        const lonARad = lonA * Math.PI / 180;
        const latARad = latA * Math.PI / 180;
        const e = Math.sqrt(2 * f - f * f);
        const N = a / Math.sqrt(1 - e * e * Math.sin(latARad) * Math.sin(latARad));
        const x = k0 * N * Math.cos(latARad) * Math.cos(lonARad - lon0);
        const y = k0 * N * Math.cos(latARad) * Math.sin(lonARad - lon0);
        const ty = new Vector3(x,y,point.z)
        return ty
    },
    //通过传入经纬度获取点位地面高程
    getUEHeightByLonLat(position:Array<Vector3>){
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const message = { function: 'getUEHeightByLonLat', sessionId: sessionId, position: position }
            stream.emitUIInteraction(message)
            const getUEHeightByLonLatReady = (data: string) => {
                const returnData = JSON.parse(data)
                if (returnData.sessionId === sessionId) {
                    resolve(returnData)
                    stream.removeResponseEventListener("getUEHeightByLonLat")
                }
            }
            stream.addResponseEventListener("getUEHeightByLonLat", getUEHeightByLonLatReady)
        })
    }
}